<template>
  <div class="login-form">
    <el-form
      :model="form"
      :rules="rules"
      ref="loginForm"
      label-position="top"
      @submit.native.prevent
    >
      <el-form-item :label="labelMap[type]" :prop="type">
        <el-input
          v-model="form[type]"
          :placeholder="`请输入${labelMap[type]}`"
          clearable
          prefix-icon="el-icon-user"
        />
      </el-form-item>

      <el-form-item label="密码" prop="password">
        <el-input
          v-model="form.password"
          placeholder="请输入密码"
          show-password
          prefix-icon="el-icon-lock"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" class="login-button" @click="handleLogin"
          >登 录</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  props: {
    type: {
      type: String,
      default: "username"
    },
    value: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      labelMap: {
        username: "用户名",
        email: "邮箱",
        phone: "手机号"
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" }
        ],
        email: [{ required: true, message: "请输入邮箱", trigger: "blur" }],
        phone: [{ required: true, message: "请输入手机号", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }]
      }
    }
  },
  computed: {
    form: {
      get() {
        return this.value
      },
      set(val) {
        this.$emit("input", val)
      }
    }
  },
  methods: {
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.$emit("submit")
        }
      })
    }
  }
}
</script>

<style scoped>
.login-form {
  text-align: left;
}

::v-deep(.el-form-item__label) {
  color: #fff;
  font-weight: 500;
  font-size: 14px;
  margin-bottom: 4px;
}

::v-deep(.el-input__inner) {
  background-color: rgba(255, 255, 255, 0.15) !important;
  border: none;
  color: #fff;
  border-radius: 10px;
  padding: 12px 15px;
}

::v-deep(.el-input__inner::placeholder) {
  color: rgba(255, 255, 255, 0.6);
}

::v-deep(.el-input__prefix) {
  color: rgba(255, 255, 255, 0.8);
}

.login-button {
  width: 100%;
  background: linear-gradient(to right, #6a5acd, #7b68ee);
  border: none;
  color: #fff;
  font-weight: bold;
  padding: 12px 0;
  border-radius: 10px;
  transition: all 0.3s;
}
.login-button:hover {
  filter: brightness(1.1);
}
</style>
